package com.czl.module_storehouse.adapter;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_storehouse.R;
import com.jakewharton.rxbinding3.view.RxView;
import com.lihang.ShadowLayout;

import java.util.List;

import io.reactivex.functions.Consumer;
import kotlin.Unit;

/**
 * @author xubin
 */
public class ManageHomeAdapter extends BaseQuickAdapter<SortBean, BaseViewHolder> {

    private boolean isEdit = true;

    public ManageHomeAdapter(int layoutResId, @Nullable List<SortBean> data) {
        super(layoutResId, data);
    }

    public boolean isEdit() {
        return isEdit;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setEdit(boolean edit) {
        isEdit = edit;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, SortBean item) {
        helper.setText(R.id.tv_goods_name, item.getSortName())
                .setText(R.id.tv_goods_code, "编号：" + item.getSortCode())
                .setText(R.id.tv_goods_norm, "规格：" + item.getSortModel())
                .setVisible(R.id.iv_delete, !isEdit)
                .setVisible(R.id.iv_edit, isEdit);
        ImageView imageView = helper.getView(R.id.iv_goods);
        GlideUtils.loadImage(getContext(), imageView, item.getSortImg(), R.mipmap.pic_default_loading, R.mipmap.pic_default_error);
        ShadowLayout shadowLayout = helper.getView(R.id.shadowLayout);
        shadowLayout.setShadowHiddenBottom(helper.getLayoutPosition() != getDefItemCount() - 1);
        helper.getView(R.id.iv_edit)
                .setOnClickListener(v -> {
                    //编辑
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(ManageHomeAdapter.this, v, helper.getLayoutPosition());
                    }
                });
        helper.getView(R.id.iv_delete)
                .setOnClickListener(v -> {
                    //删除
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(ManageHomeAdapter.this, v, helper.getLayoutPosition());
                    }
                });
    }
}
